package com.yx.frame.builder;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.List;

import com.yx.frame.builder.validate.ServiceBuilderConfigManager;

public class ViewQuerySqlTxt extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	private Table sqlTable;
	private int selectIndex;
	private Text zipCol;
	private Text sqlName;
	private Label label;
	private List dataSrc;
	private List dataSrclist;
	private Button userDefDataSrc;
	private Button addDataSrcButton;
	private Text sqlColums;
	private Label lblSql_1;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public ViewQuerySqlTxt(Shell parent, int style, Table sqlTable,
			int selectIndex) {
		super(parent, style);
		setText("Sql编辑窗口");
		this.sqlTable = sqlTable;
		this.selectIndex = selectIndex;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		shell.setSize(463, 494);
		shell.setText("查询编辑窗口");

		text = new Text(shell, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		text.setBounds(76, 154, 370, 210);
		

		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(230, 10, 48, 17);
		lblNewLabel.setText("解压列：");

		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String sqlTxt = text.getText();
				if ("".equals(sqlTxt)) {
					return;
				}
				String sqlN = sqlName.getText().trim();
				if ("".equals(sqlN)) {
					return;
				}
				String zipcol = zipCol.getText().trim();
				if (!"".equals(zipcol)) {
					try {
						String[] colArray = zipcol.split(",");
						int[] intColArry = new int[colArray.length];
						for (int i = 0; i < colArray.length; i++) {
							intColArry[i] = Integer.parseInt(colArray[i]);
							if (intColArry[i] < 0) {
								zipCol.setText("格式错误");
								return;
							}
						}
					} catch (Exception ex) {
						zipCol.setText("格式错误");
						return;
					}
				}
				String dsStr="";
				String[] items = dataSrc.getItems();
				for (int i = 0; i < items.length; i++) {
					dsStr = dsStr+ items[i]+ ",";
				}
				if (dsStr.endsWith(",")) {
					dsStr = dsStr.substring(0,
							dsStr.length() - 1);
				}
				String userDefDataStr="false";
				if(userDefDataSrc.getSelection()){
					userDefDataStr="true";
				}
				String sqlCols=sqlColums.getText().trim();
				if ("".equals(sqlCols)) {
					return;
				}
				if(selectIndex>=0){
					TableItem item = sqlTable.getItem(selectIndex);
					item.setText(1, sqlN);
					item.setText(2, sqlTxt);				
					item.setText(3, zipcol);
					item.setText(4, dsStr);
					item.setText(5, userDefDataStr);
					item.setText(6, sqlCols);
				}else{
					TableItem item = new TableItem(sqlTable, SWT.NONE);
					item.setText(new String[] { "" + sqlTable.getItemCount(),
							sqlN,sqlTxt, zipcol,dsStr,userDefDataStr,sqlCols });
				}
				shell.close();
				shell.dispose();
			}
		});
		btnNewButton.setBounds(376, 430, 71, 27);
		if(selectIndex>=0){
			btnNewButton.setText("修改");
		}else{
			btnNewButton.setText("添加");
		}

		zipCol = new Text(shell, SWT.BORDER);
		zipCol.setToolTipText("设置解压缩的列号，多个用逗号分隔");
		zipCol.setBounds(281, 7, 165, 23);
		
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setBounds(22, 10, 48, 17);
		label_1.setText("查询名：");
		
		sqlName = new Text(shell, SWT.BORDER);
		sqlName.setBounds(76, 7, 148, 23);
		
		
		label = new Label(shell, SWT.NONE);
		label.setText("数据源：");
		label.setBounds(22, 33, 48, 17);
		
		dataSrclist = new List(shell, SWT.BORDER | SWT.V_SCROLL);
		dataSrclist.setBounds(76, 34, 148, 115);
		String[] dbArray = ServiceBuilderConfigManager.getInstance().getDataSource();
		if(dbArray!=null){
			dataSrclist.setItems(dbArray);
		}		
		dataSrc = new List(shell, SWT.BORDER | SWT.V_SCROLL);
		dataSrc.setBounds(281, 34, 165, 87);
		addDataSrcButton = new Button(shell, SWT.NONE);
		addDataSrcButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx = dataSrclist.getSelectionIndex();
				if(selectIdx<0||selectIdx>=dataSrclist.getItemCount()){
					return;
				}
				String validator = dataSrclist.getItem(selectIdx);
				String[] items = dataSrc.getItems();
				boolean isExist = false;
				for (int i = 0; i < items.length; i++) {
					if (items[i].equals(validator)) {
						isExist = true;
						break;
					}
				}
				if (!isExist) {
					dataSrc.add(validator);
				}
			}
		});
		addDataSrcButton.setBounds(239, 40, 29, 27);
		addDataSrcButton.setText(">");
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx = dataSrc.getSelectionIndex();
				if(selectIdx<0||selectIdx>=dataSrc.getItemCount()){
					return;
				}
				if (selectIdx >= 0) {
					dataSrc.remove(selectIdx);
				}
			}
		});
		button.setText("<");
		button.setBounds(239, 77, 29, 27);
		
		Label lblSql = new Label(shell, SWT.NONE);
		lblSql.setText("SQL语句：");
		lblSql.setBounds(11, 154, 59, 17);
		
		userDefDataSrc = new Button(shell, SWT.CHECK);
		userDefDataSrc.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(userDefDataSrc.getSelection()){
					dataSrc.removeAll();
					dataSrc.setEnabled(false);
					addDataSrcButton.setEnabled(false);
				}else{
					dataSrc.setEnabled(true);
					addDataSrcButton.setEnabled(true);
				}
			}
		});
		userDefDataSrc.setText("用户访问时指定数据源");
		userDefDataSrc.setBounds(281, 131, 148, 17);
		
		sqlColums = new Text(shell, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		sqlColums.setBounds(76, 368, 370, 59);
		
		lblSql_1 = new Label(shell, SWT.NONE);
		lblSql_1.setText("SQL列名：");
		lblSql_1.setBounds(11, 368, 59, 17);
		
		Label lblsql = new Label(shell, SWT.NONE);
		lblsql.setBounds(75, 435, 287, 17);
		lblsql.setText("说明：解压列和SQL列名字段如果有多个请用逗号分隔");
		if(selectIndex>=0){
			TableItem item = sqlTable.getItem(selectIndex);
			text.setText(item.getText(2));
			zipCol.setText(item.getText(3));
			sqlName.setText(item.getText(1));
			String dataStr=item.getText(4);
			String[] dsArray=new String[0];
			if(!"".equals(dataStr)){
				dsArray=dataStr.split(",");
			}
			dataSrc.setItems(dsArray);
			String userDefDataStr=item.getText(5);
			if("true".equals(userDefDataStr)){
				userDefDataSrc.setSelection(true);
				dataSrc.removeAll();
				dataSrc.setEnabled(false);
				addDataSrcButton.setEnabled(false);
			}else{
				userDefDataSrc.setSelection(false);
			}
			sqlColums.setText(item.getText(6));
		}
	}
}
